package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import liumengbaio.lolzhangyoubao.Activity.InFo.UserInfo;
import liumengbaio.lolzhangyoubao.Activity.utils.HttpUtils;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/8/30.
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private Button left_title, btn_login;
    private EditText user_name_login, user_pwd_login;
    private UserInfo user_information;
    private boolean name_flag, pwd_flag;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        initView();
        iniData();
    }

    private void iniData() {
        btn_login.setEnabled(false);
        user_name_login.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()>0) {
                    name_flag = true;
                } else {
                    name_flag = false;
                }
                if (pwd_flag && name_flag) {
                    btn_login.setEnabled(true);
                }else {
                    btn_login.setEnabled(false);
                }
            }
        });
        user_pwd_login.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()>0) {
                    pwd_flag = true;
                } else {
                    pwd_flag = false;
                }

                if (pwd_flag && name_flag) {
                    btn_login.setEnabled(true);
                }else {
                    btn_login.setEnabled(false);
                }
            }
        });
        left_title.setOnClickListener(this);
        btn_login.setOnClickListener(this);

    }

    private void initView() {
        left_title = (Button) findViewById(R.id.left_title);
        btn_login = (Button) findViewById(R.id.btn_login);
        user_name_login = (EditText) findViewById(R.id.user_name_login);
        user_pwd_login = (EditText) findViewById(R.id.user_pwd_login);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_title:
                LoginActivity.this.finish();
                break;
            case R.id.btn_login:
                if (user_name_login.getText().toString().equals("13653742437") == false
                        || user_pwd_login.getText().toString().equals("123456") == false) {
                    Toast.makeText(this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                getDataFromWeb();
                finish();
                break;
        }
    }

    private void getDataFromWeb() {
        String url = "http://lol.account.zhangyoubao.com/service/rest?api=?api=user.login&game=lol&userId=&userToken=&platformVersion=400901";
        String params = "apiVersion=v1&platform=android&os=android&osVersion=19&secretSignature=NsnNh2y9R254hKbnAzPW0Pr1Hf4%3D&params%5Bname%5D=13653742437&game=lol&params%5Bpwd%5D=e10adc3949ba59abbe56e057f20f883e&sign=&platformVersion=400901&time=1472550536&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&nonce=150769&userToken=&secretVersion=v1.0&userId=&api=user.login&deviceId=JfoZvarCyLfJIqs9SRFolapk21%2FwEw63JVMaX7Y3v5w%3D&";
        try {
            HttpUtils.doPostAsyn(url, params, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    user_information = gson.fromJson(result, UserInfo.class);
                    handler.sendEmptyMessage(1);
                }
            },handler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            sendEvent(user_information);
        }
    };
    public void sendEvent(UserInfo user_information){
        EventBus.getDefault().post(user_information);
    }
}
